package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Gds;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class GdsForm extends BasePage implements Serializable {
    private GenericManager<Gds, Long> gdsManager;
    private Gds gds = new Gds();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setGdsManager(GenericManager<Gds, Long> gdsManager) {
        this.gdsManager = gdsManager;
    }

    public Gds getGds() {
        return gds;
    }

    public void setGds(Gds gds) {
        this.gds = gds;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        gdsManager.remove(gds.getId());
        addMessage("gds.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            gds = gdsManager.get(id);
        } else {
            gds = new Gds();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (gds.getId() == null || gds.getId() == 0);
        gds.setJbqk( jbqk );
        gds.setUsername(getRequest().getRemoteUser());
        gds.setOptdate(new Date());
        gds.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        
        gdsManager.save(gds);

        String key = (isNew) ? "gds.added" : "gds.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = gds.getId();
            return "edit";
        }
    }
} 